package com.FilePortal.service;

import com.FilePortal.entity.File;
import com.FilePortal.dao.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileServiceImpl implements FileService {
	//service层调用dao层
	@Autowired
	private FileMapper fileMapper;

	public void setFileMapper(FileMapper fileMapper) {
		this.fileMapper = fileMapper;
	}

	@Override
	public void addFile(File file) {
		fileMapper.addFile(file);
	}


	@Override
	public int getOnceFile(String code) {
		if (isExist(code) == true && fileMapper.getRemain(code) > 0) {
			return fileMapper.getOnceFile(code);
		} else {
			return -1;
		}//不存在此文件
	}

	@Override
	public File findOneFile(String code) {
		return fileMapper.findOneFile(code);
	}

	@Override
	public int delFile(String code) {
		if (isExist(code) == true) {
			if (fileMapper.delFile(code) == 1) {
				return 0;//成功删除1条
			}
			return -2;//删除其他条数有误
		}
		return -1;//不存在此文件
	}

	@Override
	public int getRemain(String code) {
		if (isExist(code)) {
			return fileMapper.getRemain(code);
		} else {
			return -1;//不存在此文件
		}
	}


	//返回密码
	@Override
	public String getPassword(String code) {
		if (isExist(code)) {
			return fileMapper.getPassword(code);
		} else {
			return "-1";
		}
	}

	@Override
	public boolean isExist(String code) {
		int status = fileMapper.isExistInt(code);
		return status == 1;
	}

	@Override
	public List<File> findAllFiles() {
		return fileMapper.findAllFiles();
	}
}
